package in.muton.edushare.components.tasks.schedules;

import in.muton.edushare.components.calendar.Event;
import in.muton.edushare.components.tasks.invitations.InvitationsPanel;
import in.muton.edushare.components.tasks.schedules.api.ScheduleModel;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.schedules.widgets.DateLabel;

import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.vo.ScheduleNotifications;
import com.edushare.common.vo.Users;

public class SchAcceptPanel extends EdusharePanel{
	private static final long serialVersionUID = -4409845600296734980L;

	public SchAcceptPanel(String id, IModel<Users> model) {
		super(id, model);
		this.userModel = model;
		add(CSSPackageResource.getHeaderContribution(InvitationsPanel.class,
			"Invitations.css"));
		
		ScheduleModel m = new ScheduleModel(userModel);
		add(new ListView<IModel<ScheduleNotifications>>("evtListLV", m) {
			@Override
			protected void populateItem(
					final ListItem<IModel<ScheduleNotifications>> arg0) {
				final ScheduleNotifications sn = arg0.getModelObject().getObject();
				arg0.add(new Label("evtName", sn.getSchedule().getSchTitle()));
				arg0.add(new DateLabel("start", sn.getSchedule().getSchStart(), 0));
				arg0.add(new DateLabel("end", sn.getSchedule().getSchEnd(), 0));
				arg0.add(new Label("venue", new Model<String>(){
					@Override
					public String getObject(){
						return "Venue : " + sn.getSchedule().getSchLocation();
					}
					private static final long serialVersionUID = 7749791461048538374L;
				}));
				arg0.add(new Label("repeats", Event.getRepetitionString(sn.getSchedule())));
				
				Form<Void> f = new Form<Void>("dummyForm");
				arg0.add(f);
				f.add(new Button("accept") {
					@Override
					public void onSubmit() {
						ScheduleNotifications sn = arg0.getModelObject().getObject();
						sn.setActionStatus(ScheduleNotifications.OPEN);
						getORMInterface().commitAllChanges();
					}

					private static final long serialVersionUID = 9005021645036602421L;
				});
				f.add(new Button("decline") {
					@Override
					public void onSubmit() {
						ScheduleNotifications sn = arg0.getModelObject().getObject();
						sn.setActionStatus(ScheduleNotifications.CANCELLED);
						getORMInterface().commitAllChanges();
					}

					private static final long serialVersionUID = 9005021645036602421L;
				});
			}
			private static final long serialVersionUID = -8799054605092435739L;
		});
		
		
	}
	
	@Override
	public boolean isVisible(){
		return new ScheduleModel(userModel).getObject().size() > 0;
	}
	
	private IModel<Users> userModel = null;
}
